import axios from 'axios'
import defaults from '@/config/'
// import qs from 'qs'
// import {
//   MessageBox
// } from 'element-ui'

axios.defaults.withCredentials = true

axios.defaults.headers.post['X-Requested-With'] = 'XMLHttpRequest'
axios.defaults.headers.get['X-Requested-With'] = 'XMLHttpRequest'
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
axios.defaults.responseType = 'json'
axios.defaults.timeout = defaults.axiosConfig.timeout
axios.baseURL = defaults.axiosConfig.baseURL

axios.defaults.transformRequest = [function (data) {
  return JSON.stringify(data)
  // return qs.stringify(data)
  // return data
}]
axios.defaults.validateStatus = function (status) {
  return true
}
axios.interceptors.request.use(config => {
  config.headers = {
    'Content-Type': 'application/json;charset=UTF-8',
    'Accept': 'application/json'
  }
  return config
}, error => {
  return Promise.reject(error)
})

axios.interceptors.response.use(response => {
  if (response.data && response.data.code == 200) {
    return response.data
  } else {
    // MessageBox.alert(response.data.message, '错误提示')
    return Promise.reject(response)
  }
}, error => {
  return Promise.resolve(error.response)
})

export default axios
